<?php

namespace App\Models\Orders;

use App\Models\BaseModel;

class OrdersReasonModel extends BaseModel
{
    /**
     * 与模型关联的数据表。
     *
     * @var string
     */
    protected $table = 'orders_reason';

    /**
     * 可以被批量赋值的属性。
     *
     * @var array
     */
    protected $fillable = array(
        "orders_id", "users_id", "created_at", "content"
    );

    // public $timestamps = false;

    public function aliasRable($alias)
    {
        return $this->setTable($alias);
    }

    /**
     * 查询数据需要过滤掉的属性。
     *
     * @var array
     */

    protected $hidden = array(
        'updated_at'
    );

    //返回关键字段
    public function getTableField()
    {
        return $this->fillable;
    }

    protected function serializeDate(\DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }

    /**
     * 建表语句
     */
    protected function buildTable($name = '')
    {
        $tablename = $this->table;
        if (!empty($name)) {
            $tablename = $name;
        }
        $table = "CREATE TABLE IF NOT EXISTS  $tablename (
  `id` bigint(20) UNSIGNED NOT NULL COMMENT '用户ID',
  `orders_id` int(13) UNSIGNED DEFAULT '0' COMMENT '订单表关联ID',
  `users_id` int(13) UNSIGNED DEFAULT '0' COMMENT '备注者id',
  `created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `content` varchar(250) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '备注'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='抖音备注表' ROW_FORMAT=DYNAMIC;";
        return $table;
    }

    /**
     * 插入新的字段
     */
    protected function bulidField()
    {
        return [];
    }

//关联获取审核人员的名称
    public function user()
    {
        return $this->hasOne('App\Models\Admin\AdminUsers', 'id', 'users_id');
    }

//保存备注信息
    public function saveReason($userId, $orderId, $content)
    {
        $data = [
            'users_id' => $userId,
            'orders_id' => $orderId,
            'content' => $content
        ];
        $this->insert($data);
    }
}
